import time
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By


from Page.AccountInfo import AccountInfo
from ddt import ddt,file_data

from Tools.Tools import Tools


@ddt
class AccountInfoCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.logger = Tools().get_logger("Test.AccountInfo")  # Case层专用命名空间


    def setUp(self):
        self.driver = webdriver.Edge()
        self.driver.maximize_window()
        self.po = AccountInfo(self.driver)


    def tearDown(self):
        time.sleep(3)
        self.driver.quit()

    def forward(self):
        self.logger.info("执行前置操作: 登录并进入账户管理")
        self.po.login()
        dy_login = self.po.find_element((By.XPATH, '/html/body/div[1]/div[3]/div/div[2]/div/div/p[1]/span')).text
        # 登录成功断言
        self.assertEqual('龙果支付后台', dy_login)
        # 点击账户管理
        self.po.account_manage()
        # 断言账户信息和账户历史信息
        dy2 = self.po.find_element((By.XPATH, '/html/body/div[1]/div[2]/div[2]/div[2]/div[4]/ul/li[1]/div/a')).text
        self.assertEqual('账户信息', dy2)
        dy3 = self.po.find_element((By.XPATH, '/html/body/div[1]/div[2]/div[2]/div[2]/div[4]/ul/li[2]/div/a')).text
        self.assertEqual('账户历史信息', dy3)
        self.logger.debug(f"获取到的元素文本: {dy2},{dy3}")
        self.logger.info("前置操作结果：成功进入账户管理页面")

    @file_data(Tools.resolve_path("Data/accountInfo.yaml"))
    def test_01_AccountInfo_query(self,id,exp,status):
        try:
            self.forward()
            time.sleep(1)
            # 数据驱动
            self.logger.info('---------数据驱动(账户管理查询功能)-------- ')
            self.po.query_account_params(id)
            if status == 'T':
                dy = self.po.find_element((By.XPATH,'/html/body/div[1]/div[3]/div/div[2]/div[2]/div[2]/div[1]/div[2]/div/table/tbody/tr/td[3]/div')).text
            else:
                dy = self.po.find_element((By.CSS_SELECTOR,
                                           '#navTab > div.navTab-panel.tabsPageContent.layoutBox > div:nth-child(2) > div.pageContent.j-resizeGrid > div.grid > div.gridScroller > div > table > tbody > tr > td > div')).text
            self.assertEqual(exp, dy)  # add assertion here
            self.logger.debug(f'断言实际值:{dy}')
            self.logger.info('-------本组测试结束-------')
        except Exception as e:
            self.logger.error(e)

    '''删除后再查询'''
    def test_02_AccountInfo_query_again(self):
        try:
            self.forward()
            time.sleep(1)
            self.logger.info('用例：查询成功后删除数据再次输入进行查询')
            self.po.query_again()
            dy = self.po.find_element((By.XPATH,
                                       '/html/body/div[1]/div[3]/div/div[2]/div[2]/div[2]/div[1]/div[2]/div/table/tbody/tr/td[3]/div')).text
            self.assertEqual('81733828182894952450',dy)
            self.logger.debug(f'断言实际值:{dy}')
            self.logger.info('结果：查询成功')
        except Exception as e:
            self.logger.error(e)


    '''多次点击查询'''
    def test_03_AccountInfo_many_query(self):
        try:
            self.logger.info('用例：多次点击查询')
            self.forward()
            time.sleep(1)
            self.po.many_query()
            dy = self.po.find_element((By.XPATH,
                                       '/html/body/div[1]/div[3]/div/div[2]/div[2]/div[2]/div[1]/div[2]/div/table/tbody/tr/td[3]/div')).text
            self.assertEqual('81733828182894952450', dy)
            self.logger.debug(f'断言实际值:{dy}')
            self.logger.info('结果：查询功能无异常')
        except Exception as e:
            self.logger.error(e)

    # @file_data(Tools.resolve_path("Data/accountInfo.yaml"))
    # def test_04_payment_order(self, id, exp, status):
    #     self.forward()
    #     # 数据驱动
    #     self.po.query_history_params(id)
    #     if status == 'T':
    #         dy = self.po.find_element((By.XPATH,
    #                                    '/html/body/div[1]/div[3]/div/div[2]/div[2]/div[2]/div[1]/div[2]/div/table/tbody/tr/td[3]/div')).text
    #     else:
    #         dy = self.po.find_element((By.XPATH, "//div[text()='暂无数据']")).text
    #     self.assertEqual(exp, dy)  # add assertion here


if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(AccountInfoCase)
    runner = unittest.TextTestRunner()
    # 执行
    runner.run(suite)

